//
//  ViewController.m
//  TestDemo
//
//  Created by QDSG on 2021/9/7.
//

#import "ViewController.h"
#import "Person.h"
#import "Teacher.h"

@interface ViewController ()

@property (nonatomic, assign) NSInteger num;

@property (nonatomic, strong) NSMutableArray *mArray;

@property (nonatomic, copy) void(^doWork)(void);

@property (nonatomic, copy) void(^doStudent)(void);

@property (nonatomic, strong) NSMutableArray *imageArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self test0];
    
    /// 打印：-[Person saySomething] - <ViewController: 0x7f8962d074a0>
//    Class cls = [Person class];
//    void *tt = &cls;
//    [(__bridge id)tt saySomething];
    
//    [self test1];
    
//    [self test2];
    
//    [self test3];
    
//    [self test4];
    
//    [self test5];
    
//    [self test6];
    
//    [self test7];
    
//    [self test8];
    
    [self test9];
}

/// 打印：Teacher - Teacher
- (void)test0 {
    Teacher *t = [[Teacher alloc] init];
}

/// 报错：Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
- (void)test1 {
    Class cls = [Person class];
    void *tt = &cls;
    [(__bridge id)tt saySomething];
}

/// 打印：1 3 4 5 1
- (void)test2 {
    NSObject *obj = [NSObject new];
    NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)obj));
    
    void(^block1)(void) = ^{
        NSLog(@"------%ld", CFGetRetainCount((__bridge CFTypeRef)obj));
    };
    block1();
    
    void(^__weak block2)(void) = ^{
        NSLog(@"++++++%ld", CFGetRetainCount((__bridge CFTypeRef)obj));
    };
    block2();
    
    void(^block3)(void) = [block2 copy];
    block3();
    
    __block NSObject *obj1 = [NSObject new];
    void(^block4)(void) = ^{
        NSLog(@"======%ld", CFGetRetainCount((__bridge CFTypeRef)obj1));
    };
    block4();
}

// MARK: - 多线程相关

/// 打印：>= 5
- (void)test3 {
    while (self.num < 5) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            self.num++;
        });
    }
    NSLog(@"num: %ld", self.num);
}

/// 打印：<10000
- (void)test4 {
    for (int i = 0; i < 10000; i++) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            self.num++;
        });
    }
    NSLog(@"+++num: %ld", self.num);
}

/// 打印：1 5 2 3 4
- (void)test5 {
    dispatch_queue_t queue = dispatch_queue_create("test5", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"%s -- 1", __func__);
    dispatch_async(queue, ^{
        NSLog(@"%s -- 2", __func__);
        dispatch_sync(queue, ^{
            NSLog(@"%s -- 3", __func__);
        });
        NSLog(@"%s -- 4", __func__);
    });
    NSLog(@"%s -- 5", __func__);
}

/// 死锁崩溃
- (void)test6 {
    dispatch_queue_t queue = dispatch_queue_create("test6", NULL);
    NSLog(@"%s -- 1", __func__);
    dispatch_async(queue, ^{
        NSLog(@"%s -- 2", __func__);
        dispatch_sync(queue, ^{
            NSLog(@"%s -- 3", __func__);
        });
        NSLog(@"%s -- 4", __func__);
    });
    NSLog(@"%s -- 5", __func__);
}

- (NSMutableArray *)mArray {
    if (!_mArray) {
        _mArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _mArray;
}

- (void)test7 {
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1", @"2", nil];
    self.mArray = arr;
    
    void(^block)(void) = ^{
        [arr addObject:@"3"];
        [self.mArray addObject:@"a"];
        NSLog(@"%s -- arr: %@", __func__, arr);
        NSLog(@"%s -- mArray: %@", __func__, self.mArray);
    };
    
    [arr addObject:@"4"];
    [self.mArray addObject:@"5"];
    
    arr = nil;
    self.mArray = nil;
    
    block();
}

- (void)test8 {
    __weak typeof(self) weakself = self;
    self.doWork = ^{
        __strong typeof(self) strongself = weakself;
        weakself.doStudent = ^{
            NSLog(@"%s -- %@", __func__, strongself);
        };
        weakself.doStudent();
    };
    self.doWork();
}

- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

- (void)test9 {
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(0, 0);
    
//    __weak typeof(self) weakself = self;
    for (int i = 0; i < 5000; i++) {
        dispatch_async(concurrentQueue, ^{
//            __strong typeof(self) strongself = weakself;
            NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (i % 10)];
            NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:data];
            
            dispatch_barrier_async(concurrentQueue, ^{
//                [strongself.imageArray addObject:image];
                [self.imageArray addObject:image];
            });
        });
    }
}

@end
